package com.alibaba.day0318yk.controller;

import com.alibaba.day0318yk.pojo.Kc;
import com.alibaba.day0318yk.pojo.Stu;
import com.alibaba.day0318yk.pojo.User;
import com.alibaba.day0318yk.service.KcService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @description:
 * @author: Wang
 * @create: 2024-03-18 11:37
 **/
@Controller
@RequestMapping("/kccheck")
public class KcCheckController {

    @Autowired
    private KcService kcService;

    @RequestMapping("/show")
    @ResponseBody
    private ModelAndView show(HttpServletRequest request, HttpServletResponse response,@RequestParam(defaultValue = "2")Integer pageSize,
                              @RequestParam(defaultValue = "1")Integer pageNum) throws IOException {

        User user = (User) request.getSession().getAttribute("user");

        Integer id = user.getId();
        Integer roles = user.getRoles();

        ModelAndView modelAndView = new ModelAndView();
        if (roles == 0) {
            List<Kc> kcList = kcService.show(id);
            modelAndView.addObject("kclist",kcList);
            modelAndView.setViewName("student");
        }

        /**
         * (17)如果登录人是管理员则可以查看所有的选课信息如上图（5分)
         * (18)列表支持分页功能（2分)
         */
        if (roles == 1){
            List<Kc> kcList = kcService.sele(id);

            PageHelper.startPage(pageSize,pageNum);

            for (Kc kc : kcList) {
                List<Stu> mid = kcService.seleMid(kc.getKcId());
//                System.out.println(mid);
                kc.setStus(mid);
//                kc.setNames();
            }
            modelAndView.addObject("kcList",kcList);
            PageInfo<Kc> pageInfo = new PageInfo<>(kcList, 3);
            modelAndView.addObject("pageInfo",pageInfo);
            modelAndView.setViewName("teacher");
//            response.sendRedirect("/teacher");

        }

        return modelAndView;
    }

    @RequestMapping("/deleteByid")
    @ResponseBody
    private Integer deleteByid(Integer kcId,HttpServletRequest request){

        User user = (User) request.getSession().getAttribute("user");
        Integer integer = kcService.deleteById(kcId,user.getId());
        return integer;
    }


}
